package com.binaryTree;

public class BalancedBinaryTree0302_110 {
    //递归
    public boolean isBalanced(TreeNode root) {
        return getHeight(root) != -1;
    }

    private int getHeight(TreeNode root) {
        if(root == null) return 0;

        int leftH = getHeight(root.left);
        if (leftH == -1) return -1;
        int rightH = getHeight(root.right);
        if(rightH == -1) return -1;

        return Math.abs(leftH - rightH) > 1 ? -1 : Math.max(leftH, rightH)+1;
    }

    //迭代

}
